package cn.itsource.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.domain.User;
import cn.itsource.annotation.Logs;
import cn.itsource.mapper.UserMapper;
import cn.itsource.query.UserQuery;
import cn.itsource.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Logs
    @Transactional
    @Override
    public void add(User user) {
        String salt = RandomUtil.randomString(32);
        String md5password = SecureUtil.md5(user.getPassword()+ salt);
        user.setSalt(salt);
        user.setPassword(md5password);
        userMapper.add(user);
    }
    @Logs
    @Transactional
    @Override
    public void update(User user) {
        userMapper.update(user);
    }
    @Logs
    @Transactional
    @Override
    public void del(Long id) {
        userMapper.del(id);
    }
    @Logs
    @Transactional
    @Override
    public void delList(List<Long> ids) {
            userMapper.delList(ids);
        }

    @Override
    public User get(Long id) {
        return userMapper.get(id);
    }

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public PageInfo<User> pageQuery(UserQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<User> users = userMapper.pageQuery(query);
        return new PageInfo<>(users);
    }
}
